<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>加密一个 S/MIME 消息</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.openssl-pkcs7-encrypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pkcs7_encrypt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">openssl_pkcs7_encrypt</span> &mdash; <span class="dc-title">加密一个 S/MIME 消息</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-pkcs7-encrypt-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pkcs7_encrypt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$infile</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$outfile</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$recipcerts</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$headers</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$cipherid</code><span class="initializer"> = OPENSSL_CIPHER_RC2_40</span></span>
  ]] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> 获取文件名为<code class="parameter">infile</code>的文件内容并使用 RC2 40位的密码将之加密，以至于他们只能被预期的名为<code class="parameter">recipcerts</code>的接收者阅读。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pkcs7-encrypt-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">infile</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">outfile</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">recipcerts</code></dt>

     <dd>

      <p class="para">
       一个单独的X.509证书，或者一个X.509证书的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">headers</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">headers</code> 是包含头信息的数组，在被加密后将对数据进行预处理。
      </p>
      <p class="para">
       <code class="parameter">headers</code> 可以是以头名为键值的关联数组，也可以是一个索引数组，其中每个元素都包含一个单独的标题行
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags</code>用来指定影响编码过程的选项 - 参见 <a href="openssl.pkcs7.flags.html" class="link">PKCS7
       常量</a>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">cipherid</code></dt>

     <dd>

      <p class="para">
       <a href="openssl.ciphers.html" class="link">密码常量</a>之一。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkcs7-encrypt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pkcs7-encrypt-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-941">
    <p><strong>Example #1 <span class="function"><strong>openssl_pkcs7_encrypt()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;message&nbsp;you&nbsp;want&nbsp;to&nbsp;encrypt&nbsp;and&nbsp;send&nbsp;to&nbsp;your&nbsp;secret&nbsp;agent<br />//&nbsp;in&nbsp;the&nbsp;field,&nbsp;known&nbsp;as&nbsp;nighthawk.&nbsp;&nbsp;You&nbsp;have&nbsp;his&nbsp;certificate<br />//&nbsp;in&nbsp;the&nbsp;file&nbsp;nighthawk.pem<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">Nighthawk,<br /><br />Top&nbsp;secret,&nbsp;for&nbsp;your&nbsp;eyes&nbsp;only!<br /><br />The&nbsp;enemy&nbsp;is&nbsp;closing&nbsp;in!&nbsp;Meet&nbsp;me&nbsp;at&nbsp;the&nbsp;cafe&nbsp;at&nbsp;8.30am<br />to&nbsp;collect&nbsp;your&nbsp;forged&nbsp;passport!<br /><br />HQ<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">//&nbsp;load&nbsp;key<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">"nighthawk.pem"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;message&nbsp;to&nbsp;file<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;encrypt&nbsp;it<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">openssl_pkcs7_encrypt</span><span style="color: #007700">(</span><span style="color: #DD0000">"msg.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"enc.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"To"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"nighthawk@example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;keyed&nbsp;syntax<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"From:&nbsp;HQ&nbsp;&lt;hq@example.com&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;indexed&nbsp;syntax<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Subject"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Eyes&nbsp;only"</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;message&nbsp;encrypted&nbsp;-&nbsp;send&nbsp;it!<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">"sendmail_path"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;&nbsp;enc.txt"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


</div></div></div></body></html>